package com.hq.door.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.hq.door.dao.TClassDAO;
import com.hq.door.dao.TInstituteDAO;
import com.hq.door.dao.TMajorDAO;
import com.hq.door.dao.TTeacherDAO;
import com.hq.door.dto.ClassInfo;
import com.hq.door.dto.DClass;
import com.hq.door.dto.DClassDto;
import com.hq.door.dto.StudentInfo;
import com.hq.door.dto.Teacher;
import com.hq.door.dto.TeacherDto;
import com.hq.door.pojo.TClass;
import com.hq.door.pojo.TInstitute;
import com.hq.door.pojo.TMajor;
import com.hq.door.pojo.TStudent;
import com.hq.door.pojo.TTeacher;
import com.hq.door.service.TClassService;

@Service("tClassService")
public class TClassServiceImpl extends BaseServiceImpl<TClass> implements TClassService {
	TClassDAO tClassDAO;
	@Autowired
	TMajorDAO tMajorDAO;
	@Autowired
	TTeacherDAO tTeacherDAO;
	@Autowired
	TInstituteDAO instituteDAO;

	public TClassDAO gettClassDAO() {
		return tClassDAO;
	}
	@Autowired
	public void settClassDAO(TClassDAO tClassDAO) {
		this.setBaseDao(tClassDAO);
		this.tClassDAO = tClassDAO;
	}

	@Override
	public boolean deleteselect(int[] id) {
		TClass tClass = new TClass();
		for (int i = 0; i < id.length; i++) {
			tClass = tClassDAO.get(id[i]);
			if (tClass != null) {
				tClassDAO.delete(tClass);
			}
		}
		return true;
	}

	@Override
	public boolean saveclass(DClass dClass) {
		String hql="from TMajor t where t.name=?";
		TMajor tMajor = tMajorDAO.queryOne(hql, dClass.getMajorname());
		String hql1="from TTeacher t where t.name=?";
		TTeacher tTeacher = tTeacherDAO.queryOne(hql1, dClass.getTeachername());
		String hql2="from TInstitute t where t.name=?";
		TInstitute tInstitute= instituteDAO.queryOne(hql2, dClass.getInstitutename());
		
		TClass tClass =new TClass();
		tClass = DClassDto.readMajor(dClass);
		tClass.setTMajor(tMajor);
		tClass.setTTeacher(tTeacher);
		tClass.getTMajor().setTInstitute(tInstitute);
		
		tClassDAO.save(tClass);
		
		return true;
	}
	@Override
	public List<DClass> getByPage(int type, String para, int pageStart,
			int pageSize) {
		String hql;
		List<DClass> dClasses = null;
		List<TClass> tClasses = null;
		pageStart = (pageStart - 1) * pageSize;
		switch (type) {
		case 0:
			hql="from TClass";
			tClasses = tClassDAO.findPageObjects(hql, pageStart, pageSize);
			DClass[] des = DClassDto.writeMajor(tClasses);
			dClasses= new ArrayList<DClass>();
			for (int i = 0; i < des.length; i++) {
				dClasses.add(des[i]);
			}
			break;
		case 1:
			hql ="from TClass t where t.code like ?";
			para="%"+para+"%";
			tClasses = tClassDAO.findPageObjects(hql, pageStart, pageSize,para);
			DClass[] des1 = DClassDto.writeMajor(tClasses);
			dClasses= new ArrayList<DClass>();
			for (int i = 0; i < des1.length; i++) {
				dClasses.add(des1[i]);
			}
			break;
		case 2:
			hql ="from TClass t where t.name like ?";
			para="%"+para+"%";
			tClasses = tClassDAO.findPageObjects(hql, pageStart, pageSize);
			DClass[] des2 = DClassDto.writeMajor(tClasses);
			dClasses= new ArrayList<DClass>();
			for (int i = 0; i < des2.length; i++) {
				dClasses.add(des2[i]);
			}
			break;

		default:
			return dClasses;
		}
		return dClasses;
	}
	@Override
	public int getCount(int type, String para) {
		String hql;
		int count;
		switch(type){
		case 0:
			hql="select count(t.id) from TClass t";
			count = tClassDAO.getCount(hql);
			break;
		case 1:
			hql="select count(t.id) from TClass t where t.name like ?";
			para="%"+para+"%";
			count =tClassDAO.getCount(hql, para);
		case 2:
			hql="select count(t.id) from TClass t where t.code like ?";
			para="%"+para+"%";
			count =tClassDAO.getCount(hql, para);
		default:
			return 0;
		}
		return count;
	}
	
	@Override
	public DClass getOneClass(String code) {
		String hql = "from TClass where code=?";
		TClass tClass = tClassDAO.queryOne(hql, code);
		DClass classInfo = null;
		if(tClass!=null){
			classInfo = new DClass();
			classInfo.getClass(tClass);
		}
		return classInfo;
	}


	
}
